﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoneyLibrary.Util;
using FCNS.KeepMoney.Configuration;

namespace FCNS.KeepMoney.Util
{
    struct XY
    {
        public object X;
        public decimal Yfirst ;
        public decimal Ysecond;
        public decimal Ythird;
        public decimal Yfourth;

        public XY(object x, decimal Yfirst)
        {
            X = x;
            this.Yfirst = Yfirst;
            this.Ysecond = 0;
            this.Ythird = 0;
            this.Yfourth =0;
        }

        public XY(object x, decimal Yfirst, decimal Ysecond)
        {
            X = x;
            this.Yfirst = Yfirst;
            this.Ysecond = Ysecond;
            Ythird = 0;
            Yfourth = 0;
        }

        public XY(object x, decimal Yfirst, decimal Ysecond, decimal Ythird, decimal Yfourth)
       {
           X = x;
           this.Yfirst = Yfirst;
           this.Ysecond = Ysecond;
           this.Ythird = Ythird;
           this.Yfourth = Yfourth;
       }
    }

    class ChartUtil
    {
        public static void ShowChart(ChartType pType, Chart chartPanel, List<XY> XYvalue)
        {
            chartPanel.Series.Clear();
            Program.AppConfig.Charts.DefaultChart = pType;
            switch (pType)
            {
                case ChartType.Normal:
                    ShowChartPie(chartPanel, XYvalue);
                    break;

                case ChartType.Line:
                    XYvalue = new List<XY>();
                    int days=DateTime.DaysInMonth(Program.YearShown.Year, Program.YearShown.Month);
                    for (int i = 1; i <=days ; i++)
                    {
                        List<EntryField> fs = EntryField.GetEntryField.FindAll(ef =>
                        {
                            return ef.EntryType != KMDEntryType.Transfer &&
                                ef.Date.Month == Program.YearShown.Month && ef.Date.Day == i;
                        });

                        if (fs.Count == 0 && !Program.AppConfig.Charts.LineMode)
                            continue;

                        decimal income = 0, expenses = 0;
                        foreach (EntryField ef in fs)
                            if (ef.Money>0)
                                income += ef.Money;
                            else
                                expenses += ef.Money;

                        if (!Program.AppConfig.Charts.ExpensesDisplayedAsNegative)
                            expenses = -expenses;

                        if (Program.AppConfig.Charts.HideZero && income == 0 && expenses == 0)
                            continue;

                        XYvalue.Add(new XY(i, income, expenses));
                    }

                    ShowChartLine(XYvalue, chartPanel);
                    break;

                case ChartType.Histogram:
                    XYvalue = new List<XY>();
                    for (int i = 1; i < 12; i++)
                    {
                        List<EntryField> fs = EntryField.GetEntryField.FindAll(ef => { return ef.EntryType != KMDEntryType.Transfer && ef.Date.Month == i; });

                        decimal income= 0, expenses = 0;
                        foreach (EntryField ef in fs)
                            if (ef.Money>0)
                                income += ef.Money;
                            else
                                expenses += ef.Money;

                        if (!Program.AppConfig.Charts.ExpensesDisplayedAsNegative)
                            expenses = -expenses;

                        if (Program.AppConfig.Charts.HideZero && income == 0 && expenses == 0)
                            continue;

                        XYvalue.Add(new XY(i, income, expenses));
                    }

                    ShowChartHistogram(XYvalue, chartPanel);
                    break;

                case ChartType.Pie:
                    ShowChartPie(chartPanel, XYvalue);
                    break;
            }
        }


        private static void ShowChartPie(Chart chartPanel, List<XY> XYvalue)
        {
            if (XYvalue == null)
                return;

            System.Diagnostics.Debug.Assert(XYvalue != null);
            chartPanel.Series.Add(new Series("default", 20));

            chartPanel.Series["default"]["PieDrawingStyle"] = "SoftEdge";
            chartPanel.Series["default"].ChartType = SeriesChartType.Pie;
            chartPanel.Series["default"]["PieLabelStyle"] = "Inside";
            chartPanel.Series["default"].LabelForeColor = Color.White;
            chartPanel.Series["default"].IsValueShownAsLabel = true;

            foreach (XY xy in XYvalue)
                chartPanel.Series["default"].Points.AddXY(xy.X,xy.Yfirst);
        }
        //月收入与支出(柱形) 预计收入与支出(线型)
        private static void ShowChartHistogram(List<XY> XYs, Chart chartPanelLeft)
        {
            chartPanelLeft.Series.Add(new Series(LanguageUtil.收入月, 20));
            chartPanelLeft.Series.Add(new Series(LanguageUtil.支出月, 20));

            chartPanelLeft.Series[LanguageUtil.收入月].ChartType = SeriesChartType.Column;
            chartPanelLeft.Series[LanguageUtil.支出月].ChartType = SeriesChartType.Column;

            chartPanelLeft.Series[LanguageUtil.收入月].LabelForeColor = Color.FromArgb(Program.AppConfig.Money.PlusColor);
            chartPanelLeft.Series[LanguageUtil.支出月].LabelForeColor = Color.FromArgb(Program.AppConfig.Money.MinusColor);

            chartPanelLeft.Series[LanguageUtil.收入月].IsValueShownAsLabel = true;
            chartPanelLeft.Series[LanguageUtil.支出月].IsValueShownAsLabel = true;

            chartPanelLeft.Series[LanguageUtil.收入月]["DrawingStyle"] = "Cylinder";
            chartPanelLeft.Series[LanguageUtil.支出月]["DrawingStyle"] = "Cylinder";

            foreach (XY xy in XYs)
            {
                chartPanelLeft.Series[LanguageUtil.收入月].Points.AddXY(xy.X, xy.Yfirst);
                chartPanelLeft.Series[LanguageUtil.支出月].Points.AddXY(xy.X, xy.Ysecond);
            }
        }
        //日收入与支出(线型)
        private static void ShowChartLine(List<XY> XYs, Chart chartPanelLeft)
        {
            chartPanelLeft.Series.Add(new Series(LanguageUtil.收入日, 20));
            chartPanelLeft.Series.Add(new Series(LanguageUtil.支出日, 20));

            foreach (XY xy in XYs)
            {
                chartPanelLeft.Series[LanguageUtil.收入日].Points.AddXY(xy.X, xy.Yfirst);
                chartPanelLeft.Series[LanguageUtil.支出日].Points.AddXY(xy.X, xy.Ysecond);
            }

            chartPanelLeft.Series[LanguageUtil.收入日].ChartType = SeriesChartType.Spline;
            chartPanelLeft.Series[LanguageUtil.支出日].ChartType = SeriesChartType.Spline;

            chartPanelLeft.Series[LanguageUtil.收入日].MarkerStyle = MarkerStyle.Circle;
            chartPanelLeft.Series[LanguageUtil.支出日].MarkerStyle = MarkerStyle.Circle;

            chartPanelLeft.Series[LanguageUtil.收入日].MarkerSize = 8;
            chartPanelLeft.Series[LanguageUtil.支出日].MarkerSize = 8;

            chartPanelLeft.Series[LanguageUtil.收入日].BorderWidth =3;
            chartPanelLeft.Series[LanguageUtil.支出日].BorderWidth =3;

            chartPanelLeft.Series[LanguageUtil.收入日].LabelForeColor = Color.FromArgb(Program.AppConfig.Money.PlusColor);
            chartPanelLeft.Series[LanguageUtil.支出日].LabelForeColor = Color.FromArgb(Program.AppConfig.Money.MinusColor);

            chartPanelLeft.Series[LanguageUtil.收入日].IsValueShownAsLabel = true;
            chartPanelLeft.Series[LanguageUtil.支出日].IsValueShownAsLabel = true;
        }
        ////分类统计(饼型)
        //private static void DrawChartPie(Chart chartPanelLeft, Chart chartPanelRight)
        //{
        //    chartPanelLeft.Series.Add(new Series(LanguageUtil.收入分类, 20));
        //    chartPanelRight.Series.Add(new Series(LanguageUtil.支出分类, 20));

        //    chartPanelLeft.Series[LanguageUtil.收入分类]["PieDrawingStyle"] = "SoftEdge";
        //    chartPanelRight.Series[LanguageUtil.支出分类]["PieDrawingStyle"] = "SoftEdge";

        //    chartPanelLeft.Series[LanguageUtil.收入分类].ChartType = SeriesChartType.Pie;
        //    chartPanelRight.Series[LanguageUtil.支出分类].ChartType = SeriesChartType.Pie;

        //    chartPanelLeft.Series[LanguageUtil.收入分类]["PieLabelStyle"] = "Inside";
        //    chartPanelRight.Series[LanguageUtil.支出分类]["PieLabelStyle"] = "Inside";

        //    chartPanelLeft.Series[LanguageUtil.收入分类].LabelForeColor = Color.FromArgb(Program.AppConfig.Money.PlusColor);
        //    chartPanelRight.Series[LanguageUtil.支出分类].LabelForeColor = Color.FromArgb(Program.AppConfig.Money.MinusColor);

        //    chartPanelLeft.Series[LanguageUtil.收入分类].IsValueShownAsLabel = true;
        //    chartPanelRight.Series[LanguageUtil.支出分类].IsValueShownAsLabel = true;

        //    List<GroupField> groupIncome = new List<GroupField>();
        //    List<GroupField> groupExpense = new List<GroupField>();
        //    foreach (GroupField gf in GroupField.GetGroupField)
        //        if (gf.IorE)
        //            groupIncome.Add(gf);
        //        else
        //            groupExpense.Add(gf);

        //    List<decimal> yValues = new List<decimal>();
        //    List<string> xValues = new List<string>();
        //    foreach (GroupField gf in groupIncome)
        //    {
        //        decimal d=0;
        //        foreach (EntryField f in EntryField.GetEntryField.FindAll(ef => { return ef.EntryType != KMDEntryType.Transfer && ef.GroupId == gf.Id; }))
        //            d += f.Money;

        //        yValues.Add(d);
        //        xValues.Add(gf.Name);
        //    }

        //    chartPanelLeft.Series[LanguageUtil.收入分类].Points.DataBindXY(xValues, yValues);

        //    yValues.Clear();
        //    xValues.Clear();
        //    foreach (GroupField gf in groupExpense)
        //    {
        //        decimal d = 0;
        //        foreach (EntryField f in EntryField.GetEntryField.FindAll(ef => { return ef.EntryType != KMDEntryType.Transfer && ef.GroupId == gf.Id; }))
        //            d += f.Money;

        //        yValues.Add(d);
        //        xValues.Add(gf.Name);
        //    }
        //    chartPanelRight.Series[LanguageUtil.支出分类].Points.DataBindXY(xValues, yValues);
        //}
    }
}
